﻿package cn.smbms.service.user;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import cn.smbms.dao.BaseDao;
import cn.smbms.dao.provider.ProviderMapper;
import cn.smbms.dao.user.UserDao;
import cn.smbms.dao.user.UserDaoImpl;
import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.tools.MyBatisUtil;
import sun.misc.Contended;

/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */
@Service("userService")
public class UserServiceImpl implements UserService{
	
	@Resource
	private UserMapper userMapper;
	
	public UserMapper getUserMapper() {
		return userMapper;
	}
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}
	/*private UserDao userDao;
	public UserServiceImpl(){
		userDao = new UserDaoImpl();
	}*/
	@Override
	public boolean add(User user) {
		int count = userMapper.insertUser(user);
		
		if(count == 0)
			return false;
		else
			return true;
		
		/*boolean flag = false;
		Connection connection = null;
		try {
			connection = BaseDao.getConnection();
			connection.setAutoCommit(false);//开启JDBC事务管理
			int updateRows = userDao.add(connection,user);
			connection.commit();
			if(updateRows > 0){
				flag = true;
				System.out.println("add success!");
			}else{
				System.out.println("add failed!");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				System.out.println("rollback==================");
				connection.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{
			//在service层进行connection连接的关闭
			BaseDao.closeResource(connection, null, null);
		}
		return flag;*/
		
		/*SqlSession session = null;
		int index = 0;
		try {
			session = MyBatisUtil.createSqlSession();
			
			index = session.getMapper(UserMapper.class).insertUser(user);
			System.out.println("index : " + index);
			session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		if(index == 0)
			return false;
		else
			return true;*/
	}
	@Override
	public User login(String userCode, String userPassword) {
		
		User user = userMapper.getLoginUser(userCode);
		if(null != user){
			if(!user.getUserPassword().equals(userPassword))
				user = null;
		}
		return user;
		// TODO Auto-generated method stub
		/*Connection connection = null;
		User user = null;
		try {
			connection = BaseDao.getConnection();
			user = userDao.getLoginUser(connection, userCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		
		//匹配密码
		if(null != user){
			if(!user.getUserPassword().equals(userPassword))
				user = null;
		}*/
		
		/*SqlSession session = null;
		User user = null;
		try {
			session = MyBatisUtil.createSqlSession();
			
			user = session.getMapper(UserMapper.class).getLoginUser(userCode);
			
			if(null != user){
				if(!user.getUserPassword().equals(userPassword))
					user = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return user;*/
	}
	@Override
	public List<User> getUserList(String queryUserName,int queryUserRole,int currentPageNo, int pageSize) {
		
		User user = new User();
		user.setUserName(queryUserName);
		user.setUserRole(queryUserRole);
		currentPageNo = (currentPageNo-1)*pageSize;
		List<User> userList = userMapper.findUserListByNameAndRole(user, currentPageNo, pageSize);
		
		return userList;
		/*Connection connection = null;
		List<User> userList = null;
		System.out.println("queryUserName ---- > " + queryUserName);
		System.out.println("queryUserRole ---- > " + queryUserRole);
		System.out.println("currentPageNo ---- > " + currentPageNo);
		System.out.println("pageSize ---- > " + pageSize);
		try {
			connection = BaseDao.getConnection();
			userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return userList;*/
		
		/*SqlSession session = null;
		List<User> userList = null;
		try {
			session = MyBatisUtil.createSqlSession();
			User user = new User();
			user.setUserName(queryUserName);
			user.setUserRole(queryUserRole);
			
			currentPageNo = (currentPageNo-1)*pageSize;
			userList = session.getMapper(UserMapper.class).findUserListByNameAndRole(user, currentPageNo, pageSize);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return userList;*/
	}
	@Override
	public User selectUserCodeExist(String userCode) {
		User user = userMapper.getLoginUser(userCode);
		
		return user;
		// TODO Auto-generated method stub
		/*Connection connection = null;
		User user = null;
		try {
			connection = BaseDao.getConnection();
			user = userDao.getLoginUser(connection, userCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return user;*/
		
		/*User user = null;
		SqlSession session = null;
		
		try {
			session = MyBatisUtil.createSqlSession();
			
			user = session.getMapper(UserMapper.class).getLoginUser(userCode);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return user;*/
		
	}
	@Override
	public boolean deleteUserById(Integer delId) {
		int count = userMapper.deleteUser(delId);
		
		if(count == 0)
			return false;
		else
			return true;
		/*Connection connection = null;
		boolean flag = false;
		try {
			connection = BaseDao.getConnection();
			if(userDao.deleteUserById(connection,delId) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;*/
		
		/*SqlSession session = null;
		int index = 0;
		try {
			session = MyBatisUtil.createSqlSession();		
			index = session.getMapper(UserMapper.class).deleteUser(delId);
			System.out.println("index : " + index);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}		
		if(index == 0)
			return false;
		else
			return true;*/
	}
	@Override
	public User getUserById(String id) {
		User user = userMapper.findOneUserById(Integer.parseInt(id));
		return user;
		
		// TODO Auto-generated method stub
		/*User user = null;
		Connection connection = null;
		try{
			connection = BaseDao.getConnection();
			user = userDao.getUserById(connection,id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			user = null;
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return user;*/
		
		/*User user = null;
		SqlSession session = null;
		
		try {
			session = MyBatisUtil.createSqlSession();
			
			user = session.getMapper(UserMapper.class).findOneUserById(Integer.parseInt(id));
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return user;*/
		
	}
	@Override
	public boolean modify(User user) {
		int count = userMapper.updateUser(user);
		if(count == 0)
			return false;
		else
			return true;
		// TODO Auto-generated method stub
		/*Connection connection = null;
		boolean flag = false;
		try {
			connection = BaseDao.getConnection();
			if(userDao.modify(connection,user) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;*/
		
		/*SqlSession session = null;
		int index = 0;
		try {
			session = MyBatisUtil.createSqlSession();
			
			index = session.getMapper(UserMapper.class).updateUser(user);
			
			session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		if(index == 0)
			return false;
		else
			return true;*/
	}
	@Override
	public boolean updatePwd(int id, String pwd) {
		int count = userMapper.updatePsw(id, pwd);
		
		if(count == 0)
			return false;
		else
			return true;
		/*boolean flag = false;
		Connection connection = null;
		try{
			connection = BaseDao.getConnection();
			if(userDao.updatePwd(connection,id,pwd) > 0)
				flag = true;
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;*/

		/*SqlSession session = null;
		int index = 0;
		try {
			session = MyBatisUtil.createSqlSession();
			
			index = session.getMapper(UserMapper.class).updatePsw(id, pwd);
			
			session.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		if(index == 0)
			return false;
		else
			return true;*/
	}
	@Override
	public int getUserCount(String queryUserName, int queryUserRole) {
		User user = new User();
		user.setUserName(queryUserName);
		user.setUserRole(queryUserRole);
		int count = userMapper.getUserCount(user);
		return count;
		// TODO Auto-generated method stub
		/*Connection connection = null;
		int count = 0;
		System.out.println("queryUserName ---- > " + queryUserName);
		System.out.println("queryUserRole ---- > " + queryUserRole);
		try {
			connection = BaseDao.getConnection();
			count = userDao.getUserCount(connection, queryUserName,queryUserRole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return count;*/
		/*SqlSession session = null;
		int index = 0;
		try {
			session = MyBatisUtil.createSqlSession();
			User user = new User();
			user.setUserName(queryUserName);
			user.setUserRole(queryUserRole);
			index = session.getMapper(UserMapper.class).getUserCount(user);
			
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			MyBatisUtil.closeSqlSession(session);
		}
		return index;*/
	}
	
}
